<template lang="pug">
  Row
    Col(span="16")
      Form(ref='modelForm',:model='formValidate',:rules='ruleValidate',:label-width="140")
        FormItem(label='站点：').task-model_item
          Select(v-model='formValidate.siteId',placeholder='请选择站点',:disabled='$route.query.status==="modify"')
            Option(v-for="(site,index) in sites",:value="site.siteId",:key="index") {{site.siteName}}
        FormItem(label='采集起始地址：',prop='startUrl').task-model_item
          Input(v-model="formValidate.startUrl",placeholder='采集起始地址',:maxlength="500")
          Button(type='primary',:loading='loading',v-if="matchingModelStatus",@click="handleMateSite") 匹配
        FormItem(label='更多起始地址：').task-model_item
          Input(type="textarea",:rows="6",v-model="formValidate.moreStartUrl",placeholder='更多起始地址用|隔开',:maxlength="20000",style='width:600px')
    Col(span="8")
      aside(v-if='addressData.length !== 0').pageModel-con
        span 匹配模型：
        .pageModel-item(v-for="(item, index) in addressData",:key="index") {{item.modelName}}
</template>

<script>
import asyncGather from '../../api'

export default {
  data() {
    return {
      sites: [],
      matchingModelStatus: false,
      loading: false,
      addressData: [],
      formValidate: {
        siteId: '',
        startUrl: '',
        moreStartUrl: ''
      },
      ruleValidate: {
        startUrl: [{ required: true, message: '请输入采集起始地址', trigger: 'blur' }, { type: 'url', message: '采集起始地址格式错误，非正确url格式', trigger: 'blur' }]
      }
    }
  },
  created() {
    const taskPower = JSON.parse(sessionStorage.getItem('routes')).filter(item => item.name === 'gather')[0]
    if (taskPower.children[0].meta['/task/matchingModel']) this.matchingModelStatus = true
    this.handleListSiteName()
  },
  methods: {
    handleListSiteName: async function() {
      try {
        const res = (await asyncGather.listsitename({ data: {} })).data.data
        this.sites = res
        if (this.$route.query.status === 'add') this.formValidate.siteId = res[0].siteId
      } catch (error) {}
    },
    handleMateSite() {
      const { siteId, startUrl: modelurl } = this.formValidate
      this.$refs['modelForm'].validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            this.addressData = []
            const res = (await asyncGather.matchingModel({ params: { siteId, modelurl } })).data.data
            if (res !== null && res.length !== 0) {
              this.addressData = res
            } else {
              this.$Message.warning('匹配页面模型为空')
            }
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>
